package com.hisan.freeride.home.activity;

import android.os.Handler;
import android.text.Editable;

import com.hisan.freeride.R;
import com.hisan.freeride.common.base.AppConfig;
import com.hisan.freeride.common.base.BaseActivity;
import com.hisan.freeride.common.callback.DialogCallback;
import com.hisan.freeride.common.listener.IEditTextChangeListener;
import com.hisan.freeride.common.utils.CollectionUtils;
import com.hisan.freeride.common.utils.GsonUtils;
import com.hisan.freeride.common.utils.RegexUtils;
import com.hisan.freeride.common.utils.TimeCount;
import com.hisan.freeride.common.utils.WorksSizeCheckUtil;
import com.hisan.freeride.databinding.RegisterBinding;
import com.hisan.freeride.home.model.Code;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

/**
 * 创建时间 : 2017/11/18
 * 创建人：yangyingqi
 * 公司：嘉善和盛网络有限公司
 * 备注：注册页面
 */
public class RegisterActivity extends BaseActivity<RegisterBinding>{
    private TimeCount timeCount;
    @Override
    protected int getLayoutId() {
        return R.layout.register;
    }

    @Override
    protected void initView() {
        mBinding.register.setEnabled(false);
        timeCount = new TimeCount(121000,1000);
        timeCount.setCountListener(new TimeCount.CountListener() {
            @Override
            public void onTick(Long time) {
                mBinding.sendverify.setText(String.valueOf(time/1000).concat("秒后,重发"));
                mBinding.sendverify.setTextSize(12);
                mBinding.sendverify.setEnabled(false);//设置不可点击
            }
            @Override
            public void onFinish() {
                mBinding.sendverify.setTextSize(12);
                mBinding.sendverify.setText("重新发送");
                mBinding.sendverify.setEnabled(true);
            }
        });
        //1.创建工具类对象 把要改变颜色的Button先传过去
        WorksSizeCheckUtil.textChangeListener textChangeListener = new WorksSizeCheckUtil.textChangeListener(mBinding.register);
        //2.把所有要监听的edittext都添加进去
        textChangeListener.addAllEditText(mBinding.username,mBinding.mode);
        //3.接口回调 在这里拿到boolean变量 根据isHasContent的值决定 tv 应该设置什么颜色
        WorksSizeCheckUtil.setChangeListener(new IEditTextChangeListener() {
            @Override
            public void textChange(boolean isHasContent) {
                if(isHasContent){
                    mBinding.register.setEnabled(true);
                }else{
                    mBinding.register.setEnabled(false);
                }
            }
            @Override
            public void afterChange(Editable editable) {
            }
        });
    }

    @Override
    protected void initEvent() {
        mBinding.selectXy.setOnClickListener(v -> RotationActivity.skip(this, AppConfig.agreement));
        mBinding.topBack.setOnClickListener(v -> finishActivity(true));
        mBinding.register.setOnClickListener(v -> {
            String phone=mBinding.username.getText().toString();
            String pass=mBinding.pass.getText().toString();
            String mode=mBinding.mode.getText().toString();
            if (!CollectionUtils.isNullOrEmpty(phone)&&!CollectionUtils.isNullOrEmpty(pass)
                    &&!CollectionUtils.isNullOrEmpty(mode)){
                register(phone,pass,mode);
            }else {
                showSuccessToast("请输入完整数据");
            }
        });
        //发送验证码
        mBinding.sendverify.setOnClickListener(v -> {
            String  name=mBinding.username.getText().toString();
            if (!CollectionUtils.isNullOrEmpty(name)){
                if (RegexUtils.isMobileSimple(name)){
                    getSmstoken(name);
                }else {
                    showToast("请输入正确的手机号");
                }
            } else showToast("请输入手机号");
        });
    }


    //获取短信临时token
    public void getSmstoken(final String mobile) {
        OkGo.get(AppConfig.Smstoken)
                .tag(this)
                .params("mobile",mobile)
                .execute(new DialogCallback<Object>(this){
                    @Override
                    public void onSuccess(com.lzy.okgo.model.Response<Object> response) {
                        super.onSuccess(response);
                        if (!CollectionUtils.isNullOrEmpty(response.body())){
                            if (isOk(response)){
                                Code code= GsonUtils.GsonToBean(response.body().toString(),Code.class);
                                final String token= code.smstoken();
                                new Handler().postDelayed(() -> sendsms(mobile,token),200);
                            }else {
                                showError(response);
                            }
                        }
                    }

                    @Override
                    public void onError(com.lzy.okgo.model.Response<Object> response) {
                        super.onError(response);
                        showError(response);
                    }
                });
    }
    //发送短信
    private void sendsms(String mobile,String smstoken){
        OkGo.post(AppConfig.Sendsms)
                .params("mobile",mobile)
                .params("mode",1)
                .params("sms_token",smstoken)
                .execute(new DialogCallback<Object>(this){
                    @Override
                    public void onSuccess(com.lzy.okgo.model.Response<Object> response) {
                        super.onSuccess(response);
                        if (isOk(response)){
                            showSuccessToast("发送短信成功");
                            timeCount.start();
                        }
                    }
                    @Override
                    public void onError(com.lzy.okgo.model.Response<Object> response) {
                        super.onError(response);
                        showError(response);
                    }
                });
    }

    private void register(String phone, String pass, String mode) {
        OkGo.post(AppConfig.RegisterUrl)
                .tag(this)
                .params("mobile",phone)
                .params("password",pass)
                .params("verifycode",mode)
                .execute(new DialogCallback<Object>(this){
                    @Override
                    public void onSuccess(Response<Object> response) {
                        super.onSuccess(response);
                        if (isOk(response)){
                            showSuccessToast("注册成功");
                            finishActivity(true);
                        }else {
                            showError(response);
                        }
                    }

                    @Override
                    public void onError(Response<Object> response) {
                        super.onError(response);
                        showError(response);
                    }
                });

    }

    @Override
    protected void loadData(boolean isRefresh) {}
}
